﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HYSpace
{
	public partial class DlgChangeWallThick : System.Windows.Forms.Form
	{
		public DlgChangeWallThick()
		{
			this.InitializeComponent();
		}

		public DlgChangeWallThick(ExternalCommandData cmdData, ref string message, ElementSet elements, Wall refWall)
		{
			this.InitializeComponent();
			this.m_cmdData = cmdData;
			this.m_msg = message;
			this.m_elements = elements;
			this.m_refWall = refWall;
		}

		private void btnView_Click(object sender, EventArgs e)
		{
			RevitVersionFuncs.AddToSelection(this.m_cmdData.Application.ActiveUIDocument.Selection, this.m_refWall);
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			this.m_dNewThick = ((this.tbNewThick.Text == null) ? 0.0 : Convert.ToDouble(this.tbNewThick.Text));
			if (this.m_dNewThick < 5.0)
			{
				YJKMessageBox.Show("保温层厚度太小，请检查！");
				return;
			}
			CmdSetNewThick cmdSetNewThick = new CmdSetNewThick();
			Document document = this.m_cmdData.Application.ActiveUIDocument.Document;
			Selection selection = this.m_cmdData.Application.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(selection);
			cmdSetNewThick.m_dNewThick = this.m_dNewThick;
			cmdSetNewThick.m_refWall = this.m_refWall;
			YJKRevitTools.ChangeThickness(document, cmdSetNewThick.m_refWall.WallType, cmdSetNewThick.m_dNewThick);
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(typeof(Wall), document);
			List<Element> list = new List<Element>();
			foreach (Element element in elementByClass)
			{
				Wall wall = element as Wall;
				if (wall.WallType.Name == cmdSetNewThick.m_refWall.WallType.Name)
				{
					list.Add(wall);
				}
			}
			RevitVersionFuncs.SetSelection(selection, list);
			WallInfoOperate.SaveWallThickValue(this.tbNewThick.Text);
			base.DialogResult = DialogResult.Retry;
		}

		private void DlgChangeWallThick_Load(object sender, EventArgs e)
		{
			this.tbNewThick.Text = WallInfoOperate.GetWallThickValue();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
			base.Close();
		}

		private void tbNewThick_Validating(object sender, CancelEventArgs e)
		{
			if (string.IsNullOrEmpty(this.tbNewThick.Text))
			{
				e.Cancel = true;
				return;
			}
		}

		private void tbNewThick_Leave(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.tbNewThick.Text))
			{
				YJKMessageBox.Show("保温厚度不能为空！");
				this.tbNewThick.Focus();
				this.tbNewThick.Select();
				this.tbNewThick.Text = "5";
				return;
			}
		}

		public double m_dNewThick;

		private ExternalCommandData m_cmdData;

		private string m_msg;

		private ElementSet m_elements;

		private Wall m_refWall;
	}
}
